package com.kapo.egroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kapo.egroup.entity.base.lesson.Lesson;
import com.kapo.egroup.entity.base.lesson.LessonTask;
import com.kapo.egroup.entity.base.lesson.UserLessonRecord;
import com.kapo.egroup.entity.base.project.Project;
import com.kapo.egroup.entity.base.user.User;
import com.kapo.egroup.mapper.*;
import com.kapo.egroup.service.LessonService;
import com.kapo.egroup.service.LessonTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @Title LessonTaskServiceImpl
 * @Description TODO
 * @Author 86159
 * @Date 2023-04-01 20:47
 **/
@Service
public class LessonServiceImpl extends ServiceImpl<LessonMapper, Lesson> implements LessonService {
    @Autowired
    UserLessonRecordMapper userLessonRecordMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ProjectMapper projectMapper;

    @Override
    public Lesson getLessonById(Long lessonId) {
        return baseMapper.selectById(lessonId);
    }

    @Override
    public List<UserLessonRecord> getLessonsByStudentId(String studentId) {
        QueryWrapper<UserLessonRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        return userLessonRecordMapper.selectList(queryWrapper);
    }

    @Override
    public void updateLessonRecordByRecordId(UserLessonRecord userLessonRecord){
        userLessonRecordMapper.updateById(userLessonRecord);
    }

    @Override
    public UserLessonRecord getLessonRecordByUserIdAndProjectId(Long userId, Long projectId){
        User user = userMapper.selectById(userId);
        Project project = projectMapper.selectById(projectId);
        QueryWrapper<UserLessonRecord> queryWrapper = new QueryWrapper<>();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("student_id", user.getStudentId());
        queryParamsMap.put("lesson_id", project.getLessonTaskId());
        queryWrapper.allEq(queryParamsMap);
        return userLessonRecordMapper.selectOne(queryWrapper);
    }


}
